<?php
class pContentAction extends pBaseAction{

    //授权栏目
    protected $allocateCategorys;
    //栏目及过滤SQL串
    protected $filterSql;

    public function _initialize(){
        if(false===array_key_exists('allocateCategorys', $_SESSION) || ! $_SESSION['allocateCategorys']){
            $this->getCategoryFilters();
        }
        $this->allocateCategorys=$_SESSION['allocateCategorys'];
        $this->filterSql=$_SESSION['postCategoryFilter'];
    }

    public function index(){
        //查询文档
        $prefix=C('db_prefix');
        $postTable=$prefix.'post p';
        $dao=new Model();
        //分页
        $countRs=$dao->field('count(id) postcount')->where($this->filterSql)->table($postTable)->find();
        $count=intval($countRs['postcount']);
        if($count>0){
            require LIB_PATH.'Util/PageEx.class.php';
            $p=new PageEx($count,15);
            $this->assign('list',$dao->field('p.id,p.title,p.created,u.account poster,c.name typename,p.status,p.thumb')
                                    ->table($postTable)
                                    ->join($prefix.'category c on c.id=p.category')
                                    ->join($prefix.'user u on u.id=p.userid')
                                    ->limit($p->firstRow.','.$p->listRows)
                                    ->order('p.id desc')
                                    ->where($this->filterSql)
                                    ->findAll()
                );
            $this->assign('page',$p->show());
            $this->assign('postConfig', include CONFIG_PATH.'PostMeta.php');
        }
        $this->display();
    }

    public function add(){
        $this->assign('allocateCategorys',$this->allocateCategorys);
        $this->display('DefaultArticle/add');
    }

    public function save(){
        $dao=new PostModel();
        $vo=$dao->savePost();
        if($vo){
            //执行审核操作
            $this->ok('保存文档成功','成功保存了文档:<b>'.$vo['title'].'</b>',array(
                array('转到文档列表','index'),
                array('继续添加文档','add')
            ));
        }else{
            $this->fail('保存文档失败');
        }
    }

    public function edit(){
        $postid=$_GET['post'];
        if(false===is_numeric($postid)){
            return $this->fail('编辑文档失败','无法找到文档');
        }
        $dao=new PostModel();
        $postRs=$dao->where('id='.$postid)->find();
        if(!$postRs){
            return $this->fail('编辑文档失败', '无法找到文档');
        }
        //编辑前处理
        $postMetaDao=new PostMetaModel();
        $this->assign('modelRs', $postMetaDao->beforeEdit($postRs));

        $this->assign('postRs', $postRs);
        $this->assign('allocateCategorys',$this->allocateCategorys);
        $this->display($postRs['model'].'/edit');
    }

    /**
     * 更新文档...ajax方式进行
     */
    public function update(){
        noCache();
        $dao=new PostModel();
        $vo=$dao->updatePost();
        if($vo){
            echo '{result:"success"}';
        }else{
            echo '{result:"fail",msg:"'.$dao->getError().'"}';
        }
    }

    public function removal(){
    }

    public function audit(){
    }

    //获取授权栏目，并且生成一个SQL语句
    private function getCategoryFilters(){
        $usr=$_SESSION[C('USER_AUTH_KEY')];
        $dao=new CategoryModel();
        if(in_array($usr['roleName'],C('not_auth_role'))){ //是系统管理员，对所有栏目有操作权限
            $allocateCategorys=$dao->field('id,name,level')->where("status<3 and jump='' and id>1")->order('lft')->findAll();
            $_SESSION['postCategoryFilter']="(p.category>0)";
        }else{
            $prefix=C('db_prefix');
            $allocateCategorys=$dao->field('c.id,c.level,c.name,cu.allocate')
                                        ->table($prefix.'category_user cu')
                                        ->join($prefix.'category c on c.id=cu.category')
                                        ->where("cu.userid='{$usr['id']}' and cu.category>0 and c.status<3 and c.jump=''")
                                        ->findAll();
            $uid=$usr['id'];
            $sql=array();
            foreach($allocateCategorys as $row){
                if('y'===$row['allocate']) $sql[]="(p.category={$row['id']})";
                else $sql[]="(p.category={$row['id']} and p.userid={$uid})";    //没有全部权限，仅能对自己的栏目进行操作
            }
            $_SESSION['postCategoryFilter']='('.implode(' or ', $sql).')';
        }
        $_SESSION['allocateCategorys']=$allocateCategorys;
    }

}
